#===============================================================================
# Simple Pygame UI - Demonstration
# Copyright 2009 Michael Pratt
# Licensed exclusively as a personal code sample to 1st Playable Productions, LLC
#===============================================================================

import pygame; from pygame.locals import *
from pgx import ui

class UITest(object):
    '''Class UITest
    Simple class to test SUI abilities.
    Once created, execute UITest.run()
    '''
    def __init__(self):
        '''UITest.__init__:
        Initialization and setup of pygame display.
        Arguments: None
        '''
        self.screen = pygame.display.set_mode((800,600))
        self.uisurf = self.screen.convert_alpha()
        pygame.display.set_caption("Simple Pygame UI Demo - Michael Pratt")
        
        self.ui = ui.manager.WindowManager(self.uisurf)
        self.wblock1 = ui.simple.WBlock("Hiya", (255, 0, 0, 100), rect = (20, 20, 30, 20))
        self.wblock2 = ui.simple.WBlock("Hiya2", (0, 255, 0, 100))
        self.wblock2.size = (25, 30)
        self.wblock3 = ui.simple.WBlock("Hiya3", (0, 0, 255, 100), rect = (40, 10, 50, 60))
        self.wimage = ui.simple.WImage("Image", "images/Potato.png", z=-1)
        self.wimage.center = self.ui.root.center
        self.ui.add(self.wblock1, self.wblock2, self.wblock3, self.wimage)
        
        self.running = False
        
    def __del__(self):
        '''UITest.__del__:
        Destroy the pygame display or the application can hang or cause memory leaks.
        Arguments: None
        '''
        pygame.quit()
        
    def loop(self, event):
        '''UITest.loop:
        A single iteration of the main game loop, called whenever a new event occurs.
        Arguments: pygame.event.Event event
        '''
        self.ui.paint()
        self.screen.blit(self.uisurf, (0, 0))
        pygame.display.flip()
        self.screen.fill((255, 255, 255))
        self.uisurf.fill((0, 0, 0, 0))
        
        self.wblock2.topleft = pygame.mouse.get_pos()
        
        gui_handled = self.ui.injectEvent(event)
        if not gui_handled:
            if event.type == QUIT:
                print "Button Exit!"
                self.running = False
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    print "Escape Exit!"
                    self.running = False
    
    def run(self):
        '''UITest.run:
        Executes the main game loop.
        Arguments: None
        '''
        self.running = True
        while self.running:
            event = pygame.event.wait()
            self.loop(event)
        
main = UITest()
main.run()
